﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;

namespace Utility
{
    public class RandomManage
    {
        public static int GetRandomNumber(int seed, int incMin, int incMax)
        {
            Random rand = new Random(seed);
            return rand.Next(incMin, incMax + 1);
        }

        public static string GetRandomString(string source)
        {
            int randNum = GetRandomInt(0, 1000000);
            return randNum.ToString() + source;
        }

        public static int globalSeed = 0;
        public static int GetRandomInt(int incMin, int incMax)
        {
            int randNum = 0;
            Random rand = new Random((int)DateTime.Now.Ticks - globalSeed);
            randNum = rand.Next(incMin, incMax + 1);
            globalSeed++;

            return randNum;
        }


        /*--------------Password Generator---------*/
        private static char[] characterArray = "abcdefzABCDEFGHIJXYZ0123456789".ToCharArray();

        private static Random randNum = new Random();

        static char GetRandomCharacter()
        {
            return characterArray[(int)((characterArray.GetUpperBound(0) + 1) * randNum.NextDouble())];
        }

        /// <summary>
        /// Generate random string
        /// </summary>
        /// <param name="len">lenght of string need to generate</param>
        /// <returns>random string with lenght is 'len'</returns>
        public static string GenerateRandomString(int len)
        {
            StringBuilder sb = new StringBuilder();
            sb.Capacity = len;
            for (int count = 0; count <= sb.Capacity - 1; count++)
            {
                sb.Append(GetRandomCharacter());
            }
            if ((sb != null))
            {
                return sb.ToString();
            }
            return "123ABDDEF";
        }

        /// <summary>
        /// Random File Name
        /// </summary>
        /// <returns></returns>
        public static string RandomFileName()
        {
            string fileName = (DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString());
            Random rand = new Random((int)DateTime.Now.Ticks);
            int a = rand.Next();

            fileName += a.ToString();
            return fileName;
        }
    }
}